home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1999 February
/
Macworld (1999-02).dmg
/
Cinema 4D GO demo
/
Gumption Plug-ins
/
Plug-ins
/
Freeware
/
Raw
/
RAW.COF
Wrap
Text File
|
1998-03-03
|
7KB
|
412 lines
// Simple Loader/Saver for POV-Ray RAW files
// (c) Christian Losch
// 1997 Maxon Computer GmbH
var fac=50.0;
Identify(probe,name)
{
var s,l = sizeof(name);
if (l<5) return FALSE;
s = strmid(name,l-4,4);
return
strcmp(s,".RAW")==0 ||
strcmp(s,".RaW")==0 ||
strcmp(s,".RAw")==0 ||
strcmp(s,".Raw")==0 ||
strcmp(s,".rAW")==0 ||
strcmp(s,".raW")==0 ||
strcmp(s,".rAw")==0 ||
strcmp(s,".raw")==0;
}
Edit()
{
var d = new(SimpleDialog);
d->SetTitle("Pov-Ray RAW");
d->SetData(0,"Version 1.0",FIELD_STRING,0,1,"----");
d->SetData(1,"Factor",FIELD_FLOAT,0.01,1000000,fac);
if (!d->DoDialog()) return;
fac=d->GetData(1);
return TRUE;
}
// ******************************* Save **************************
AppendLF(name)
{
var txt=new(string,2);
txt[0]=0x0D;
txt[1]=0x0A;
return stradd(name,txt);
}
WriteIt(file,p1,p2,p3)
{
var t;
p1 /= fac;
p2 /= fac;
p3 /= fac;
t = tostring(p1.x);
t=stradd(t," ",tostring(p1.y));
t=stradd(t," ",tostring(p1.z));
t=stradd(t," ",tostring(p2.x));
t=stradd(t," ",tostring(p2.y));
t=stradd(t," ",tostring(p2.z));
t=stradd(t," ",tostring(p3.x));
t=stradd(t," ",tostring(p3.y));
t=stradd(t," ",tostring(p3.z));
t = AppendLF(t);
file->Write(t,sizeof(t));
}
SaveObject(op,file)
{
while (op)
{
if (getclass(op)==PolygonObject)
{
var i,d,v,mg;
var t,q,name,p1,p2,p3,p4;
mg = new(Matrix); if(!mg) return FALSE;
t = new(Triangle); if (!t) return FALSE;
q = new(Quadrangle); if (!q) return FALSE;
d = op->GetTriangleNumber();
v = op->GetQuadrangleNumber();
// write objects name
name = AppendLF(op->GetName());
file->Write(name,sizeof(name));
op->GetMg(mg);
// write triangles
for (i=0; i<d; i++)
{
op->GetTriangle(i,t);
p1 = mg->MulP(op->GetPoint(t->a));
p2 = mg->MulP(op->GetPoint(t->b));
p3 = mg->MulP(op->GetPoint(t->c));
WriteIt(file,p1,p2,p3);
}
// write quadrangles
for (i=0; i<v; i++)
{
op->GetQuadrangle(i,q);
p1 = mg->MulP(op->GetPoint(q->a));
p2 = mg->MulP(op->GetPoint(q->b));
p3 = mg->MulP(op->GetPoint(q->c));
p4 = mg->MulP(op->GetPoint(q->d));
WriteIt(file,p1,p2,p3);
WriteIt(file,p3,p4,p1);
}
}
if (!SaveObject(op->GetDown(),file)) return FALSE;
op = op->GetNext();
}
return TRUE;
}
Save(file,doc,obj,mat,env,alert)
{
var ok;
println("Saving RAW");
if (!file->Open(1)) return FALSE;
ok=SaveObject(doc->GetFirstObject(),file);
file->Close();
println("Ready");
return ok;
}
// ******************************* Load **************************
struct TRI
{
TRI();
var p1,p2,p3;
};
TRI::TRI()
{
p1 = p2 = p3 = vector(0,0,0);
}
GetNextW(s,p)
{
var i,size = sizeof(s);
if (p>=size) return p;
// search for first whitespace
if (s[p]==0x20) return p;
for (i=p; i<size; i++)
{
if (s[i]==0x20) break;
}
return i;
}
GetNextN(s,p)
{
var i,size = sizeof(s);
if (p>=size) return p;
// search for first non whitespace
if (s[p]!=0x20) return p;
for (i=p; i<size; i++)
{
if (s[i]!=0x20) break;
}
return i;
}
GetName(s)
{
var i,count=0,name,size;
size = sizeof(s);
// count
for (i=0; i<size; i++)
{
if (s[i]!=0x20 && s[i]!=0x0A && s[i]!=0x0D)
count++;
else
break;
}
name = new(string,count);
// fill
for (i=0; i<count; i++)
{
name[i] = s[i];
}
return name;
}
GetTriangle(s)
{
var i,p1,p2,t = new(TRI);
p1=GetNextN(s,0 ); p2=GetNextW(s,p1); t->p1.x = evaluate(s,p1,p2-p1);
p1=GetNextN(s,p2+1); p2=GetNextW(s,p1); t->p1.y = evaluate(s,p1,p2-p1);
p1=GetNextN(s,p2+1); p2=GetNextW(s,p1); t->p1.z = evaluate(s,p1,p2-p1);
p1=GetNextN(s,p2+1); p2=GetNextW(s,p1); t->p2.x = evaluate(s,p1,p2-p1);
p1=GetNextN(s,p2+1); p2=GetNextW(s,p1); t->p2.y = evaluate(s,p1,p2-p1);
p1=GetNextN(s,p2+1); p2=GetNextW(s,p1); t->p2.z = evaluate(s,p1,p2-p1);
p1=GetNextN(s,p2+1); p2=GetNextW(s,p1); t->p3.x = evaluate(s,p1,p2-p1);
p1=GetNextN(s,p2+1); p2=GetNextW(s,p1); t->p3.y = evaluate(s,p1,p2-p1);
p1=GetNextN(s,p2+1); p2=GetNextW(s,p1); t->p3.z = evaluate(s,p1,p2-p1);
t->p1 *= fac;
t->p2 *= fac;
t->p3 *= fac;
return t;
}
IsNumber(c)
{
return c=='0' || c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9' ||
c=='-' || c=='+';
}
ReadLine(file,max)
{
var count,s,pos,c,i;
c=new(string,1);
pos = file->GetPos();
count=0;
// count characters
do
{
file->Read(c,1);
count++;
}
while (c[0]!=0x0A && c[0]!=0x0 && (pos+count)<max);
file->SetPos(pos);
// load characters
s=new(string,count);
for (i=0; i<count; i++)
{
file->Read(c,1);
s[i]=c[0];
}
return s;
}
// 0=nothing
// 1=triangle
// 2=group
IdentLine(s)
{
var i,size=sizeof(s);
for (i=0; i<size; i++)
{
if (s[i]!=' ')
{
if (IsNumber(s[i]))
return 1;
else
return 2;
}
}
return 0;
}
CountLines(file,max)
{
var s,count,ok,pos;
pos = file->GetPos();
ok=TRUE;
count=0;
do
{
s = ReadLine(file,max);
switch (IdentLine(s))
{
case 1: count++; break;
case 2: ok=FALSE;
}
}
while (ok);
file->SetPos(pos);
return count;
}
Load(file,doc,obj,mat,env,mecker)
{
var max,op,e,t,s,lines;
var pn,en,tn; // counter for points,edges,triangles
println("Loading RAW");
if (!file->Open(0)) return FALSE;
max = file->Len();
e = new(Edge);
t = new(Triangle);
do
{
s = ReadLine(file,max);
switch (IdentLine(s))
{
case 0 : // do nothing break;
case 2: // Object name, new group
{
// update last object
if (getclass(op)==PolygonObject) op->UpdateObject();
lines = CountLines(file,max);
// generate new object
op = doc->NewPolygonObject(GetName(s),NULL,NULL,lines*3,lines*3,lines,0);
pn=en=tn=0;
}
break;
case 1:
{
// alloc new object the first time
if (getclass(op)!=PolygonObject)
{
// count triangles
lines = CountLines(file,max);
// generate new object
op = doc->NewPolygonObject("Raw",NULL,NULL,lines*3,lines*3,lines,0);
pn=en=tn=0;
}
if (getclass(op)==PolygonObject)
{
var tri;
tri = GetTriangle(s);
op->SetPoint(pn++,tri->p1);
op->SetPoint(pn++,tri->p2);
op->SetPoint(pn++,tri->p3);
t->a = pn-3;
t->b = pn-2;
t->c = pn-1;
op->SetTriangle(tn++,t);
e->a = pn-3;
e->b = pn-2;
op->SetEdge(en++,e);
e->a = pn-2;
e->b = pn-1;
op->SetEdge(en++,e);
e->a = pn-1;
e->b = pn-3;
op->SetEdge(en++,e);
}
}
}
}
while (file->GetPos()<max);
// update last object
if (getclass(op)==PolygonObject) op->UpdateObject();
file->Close();
println("Ready");
return TRUE;
}
main()
{
RegisterFilterHook(50002,"POV-Ray RAW","Identify","Load","Save","Edit");
}